package com.qing.demo.netty.demo1.bio;

import com.qing.demo.netty.demo1.bio.server.BioServerHandler;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.logging.Logger;

/**
 * @author caozhipeng
 * @date 2025/7/22
 */
public abstract class ChannelAdapter extends Thread{

    private Socket  socket;
    private Charset charset;
    private ChannelHandler channelHandler;
    protected Logger logger=Logger.getLogger(ChannelAdapter.class.getName());


    public ChannelAdapter(Socket socket, Charset charset) {
        this.socket = socket;
        this.charset = charset;
        while (!socket.isConnected()){
            break;
        }
        channelHandler=new ChannelHandler(this.socket,charset);
        channelActive(channelHandler);
    }

    public void run(){
      try {
          BufferedReader input = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), charset));
          String str = null;
//          客户端输入消息换行符结束 \n
          while ((str = input.readLine()) != null) {
              channelRead(channelHandler, str);
          }
      }catch (Exception e){
          e.printStackTrace();
      }

    }
//  链接通知抽象类
    public abstract void channelRead(ChannelHandler channelHandler, String str);
//   读取消息抽象类
    public abstract void channelActive(ChannelHandler channelHandler) ;
}
